<!-- 调拨申请管理 -->
<template>
  <div class="mod-config">
    <el-button-group style="margin-bottom: 10px;">
      <el-button type="primary" icon="el-icon-search" @click="searchHandle">查询</el-button>
      <el-button type="primary" icon="el-icon-delete" @click="searchReset">清空</el-button>
      <el-button v-if="permissions.eims_eimsallocationapply_add" icon="el-icon-plus" type="primary" @click="addOrUpdateHandle()">新增</el-button>
    </el-button-group>
    <el-form inline :model="dataForm" label-width="100px" @keyup.enter.native="getDataList">
      <el-form-item label="单据编号">
        <el-input v-model="dataForm.billId"></el-input>
      </el-form-item>
      <el-form-item label="申请部门">
        <el-select v-model="dataForm.outDeptId">
          <el-option v-for="item in deptOptions" :value="item.id" :label="item.name" :key="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="制单人">
        <el-input v-model="dataForm.createUsername"></el-input>
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="dataForm.memo"></el-input>
      </el-form-item>
      <el-form-item label="单据日期">
        <el-row>
          <el-col :span="11">
            <el-date-picker v-model="dataForm.createTimeStart" style="width: 100%;" value-format="yyyy-MM-dd"></el-date-picker>
          </el-col>
          <el-col class="line" :span="2" style="text-align: center;">-</el-col>
          <el-col :span="11">
            <el-date-picker v-model="dataForm.createTimeEnd" style="width: 100%;" value-format="yyyy-MM-dd" ></el-date-picker>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item label="审核人">
        <el-input v-model="dataForm.auditUserName"></el-input>
      </el-form-item>
      <el-form-item label="审核状态">
        <el-select v-model="dataForm.applyStatus">
          <el-option value=1 label="已审核"></el-option>
          <el-option value=0 label="未审核"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="审核日期">
        <el-row>
          <el-col :span="11">
            <el-date-picker v-model="dataForm.auditTimeStart" style="width: 100%;" value-format="yyyy-MM-dd"></el-date-picker>
          </el-col>
          <el-col class="line" :span="2" style="text-align: center;">-</el-col>
          <el-col :span="11">
            <el-date-picker v-model="dataForm.auditTimeEnd" style="width: 100%;" value-format="yyyy-MM-dd"></el-date-picker>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item label="商品名称">
        <el-input v-model="dataForm.viewGoodsName"></el-input>
      </el-form-item>
      <el-form-item label="商品代码">
        <el-input v-model="dataForm.goodsCode"></el-input>
      </el-form-item>
    </el-form>

    <div class="avue-crud">
      <el-table :data="dataList" border v-loading="dataListLoading" :max-height="tableHeight">
        <el-table-column prop="billId" label="申请单号" width="175"></el-table-column>
        <el-table-column prop="linkBillId" label="关联订单" width="175"></el-table-column>
        <el-table-column prop="outDeptName" label="申请部门" width="120"></el-table-column>
        <el-table-column prop="inDeptName" label="接收部门" width="120"></el-table-column>
        <el-table-column prop="createTime" label="制单日期" width="155"></el-table-column>
        <el-table-column prop="auditTime" label="审核日期" width="155"></el-table-column>
        <el-table-column prop="memo" label="备注" show-overflow-tooltip></el-table-column>
        <el-table-column label="操作" width="180" fixed="right">
          <template slot-scope="scope">
            <div v-if="scope.row.applyStatus == '1'">
              <el-button type="text" icon="el-icon-s-order" @click="checkDeatil(scope.row.id)">详细</el-button>
              <el-button v-if="permissions.eims_eimsallocationapply_edit" type="text" icon="el-icon-s-opportunity" disabled="disabled">修改</el-button>
              <el-button v-if="permissions.eims_eimsallocationapply_del" type="text" icon="el-icon-delete" disabled="disabled">删除</el-button>
            </div>
            <div v-else>
              <el-button type="text" icon="el-icon-s-order" @click="checkDeatil(scope.row.id)">详细</el-button>
              <el-button v-if="permissions.eims_eimsallocationapply_edit" type="text" icon="el-icon-s-opportunity" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
              <el-button v-if="permissions.eims_eimsallocationapply_del" type="text" icon="el-icon-delete" @click="deleteHandle(scope.row.id)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="avue-crud__pagination">
      <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        background
        layout="total, sizes, prev, pager, next">
      </el-pagination>
    </div>
    <!-- 弹窗, 新增 -->
    <table-form v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></table-form>
    <goods-detail v-if="goodsDetailVisible" ref="goodsDetail" @updateDataList="getDataList"></goods-detail>
  </div>
</template>

<script>
  import {allocationApplyFetchList,allocationApplyDel,allocationApplyDetail} from '@/api/eims/allocation-apply.js'
  import TableForm from './table-form'
  import GoodsDetail from './detail'
  import {mapGetters} from 'vuex'
  import { getDeptInfo } from "@/api/admin/dept"

  export default {
    data () {
      return {
        dataForm: { key: '' },
        deptOptions: [],
        dataList: [],
        pageIndex: 1,
        pageSize: 100,
        totalPage: 0,
        dataListLoading: false,
        addOrUpdateVisible: false,
        goodsDetailVisible: false
      }
    },
    components: { TableForm, GoodsDetail },
    created () {
      this.getDataList(),
      this.getDeptList()
    },
    computed: {
      ...mapGetters(['permissions']),
      tableHeight: function(){
        return window.innerHeight - 260
      }
    },
    methods: {
      searchHandle(){
        this.pageIndex = 1
        this.getDataList()
      },
      //  清空搜索表单
    searchReset() {
      for (let key in this.dataForm) {
        this.dataForm[key] = "";
      }
    },
      // 获取数据列表
      getDataList() {
        let params1 = { current: this.pageIndex, size: this.pageSize };
        let params2 = this.filterParams(this.dataForm);
        this.dataListLoading = true
        allocationApplyFetchList(Object.assign(params1, params2)).then(response => {
          if(response.data.code == '0'){
           this.dataList = response.data.data.records
           this.totalPage = response.data.data.total
          }else{
            this.$message.error(response.data.msg)
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => { this.$refs.addOrUpdate.init(id)})
      },
      // 删除
      deleteHandle (id) {
        this.$confirm('是否确认删除ID为' + id, '提示', {confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning'}).then(function () {
          return allocationApplyDel(id)
        }).then(response => {
          if(response.data.code == '0'){
           this.$message.success('删除成功')
          }else{
            this.$message.error(response.data.msg)
          }
          this.getDataList()
        })
      },
      // 查看详情
      checkDeatil (id) {
        allocationApplyDetail(id).then(result => {
          this.goodsDetailVisible = true
          this.$nextTick(() => {
            this.$refs.goodsDetail.init(id)
          })
        })
      },
      //获取部门列表
    getDeptList() {
      getDeptInfo().then(response => {
        if(response.data.code == '0'){
         this.deptOptions = response.data.data;
        }else{
          this.$message.error(response.data.msg)
        }
      });
    },
    }
  }
</script>
